﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using Castle.Core.Configuration;
using Castle.Core.Configuration.Xml;

namespace Inovout.Web
{
    internal class ContentResource
    {
        internal string Uri { get; private set; }
        internal string CdnUri { get; private set; }
        internal ContentResource[] IncludeContentResources { get; private set; }
        internal string[] IncludeContentResourceUris
        {
            get { return IncludeContentResources.Select(c => c.Uri).ToArray(); }
        }
        internal static ContentResource[] ScriptResources { get; private set; }
        internal static ContentResource[] StyleResources { get;private set;}

        internal static void Load(IApplication application)
        {
            if (application.IsDebug && File.Exists(Path.Combine(application.Path, "Configs/ContentResource.debug.config")))
            {
                LoadConfig(Path.Combine(application.Path, "Configs/ContentResource.debug.config"));
            }
            else if (File.Exists(Path.Combine(application.Path, "Configs/ContentResource.config")))
            {
                LoadConfig(Path.Combine(application.Path, "Configs/ContentResource.config"));
            }
            else
            {
                StyleResources=new ContentResource[0];
                ScriptResources=new ContentResource[0];
            }
        }
        internal static void LoadConfig(string contentResourceConfigFileName)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(contentResourceConfigFileName);
            IConfiguration contentResourceConfiguration = XmlConfigurationDeserializer.GetDeserializedNode(xmlDocument.DocumentElement);
  
            StyleResources = ReadContentConfig(contentResourceConfiguration.Children.SingleOrDefault(c=>c.Name.Equals("styles")));
            ScriptResources = ReadContentConfig(contentResourceConfiguration.Children.SingleOrDefault(c=>c.Name.Equals("scripts")));

        }
        internal static ContentResource[] ReadContentConfig(IConfiguration contentsConfiguration)
        {
            if (contentsConfiguration == null)
            {
                return new ContentResource[0];
            }
            else
            {
                List<ContentResource> contentResources = new List<ContentResource>();
                foreach (IConfiguration contentConfiguration in contentsConfiguration.Children)
                {
                    ContentResource contentResource = new ContentResource {Uri=contentConfiguration.Attributes["uri"],CdnUri=contentConfiguration.Attributes["cdnUri"],IncludeContentResources=ReadContentConfig(contentConfiguration) };
                    contentResources.Add(contentResource);
                }

                return contentResources.ToArray();
            }
        }
    }
}
